#include <gl/glew.h>
#include "TileSet.h"
#include "Game.h"
#include <iostream>

using namespace std;
TileSet::TileSet(string & ruta, bool alpha,int anchoTile, int altoTile, int numeroInicial):Textura(ruta,alpha)
	{
		inicial = numeroInicial;

		
		totalTiles = (int)(this->getAncho() / anchoTile) * (this->getAlto() / altoTile);

		tiles = new Tile*[totalTiles];

		memset(tiles,0,totalTiles);

		float anchoTileUnidad = this->getUnidadAncho()/(this->getAncho()/anchoTile);
		float altoTileUnidad = this->getUnidadAlto()/(this->getAlto()/altoTile);

		for (float y = 0; y < this->getAlto() / altoTile; y++){
			for (float x = 0; x < this->getAncho() / anchoTile; x++){
				
				short idTile = numeroInicial + (y*(this->getAncho() / anchoTile)) + x;
				tiles[idTile - numeroInicial] = new Tile((x * anchoTileUnidad),(y * altoTileUnidad),((x * anchoTileUnidad) + anchoTileUnidad),((y * altoTileUnidad)+altoTileUnidad));
				}
		}
	}


void TileSet::renderTile( float x1, float y1, float x2, float y2, short IDtile )
	{
	short x = IDtile;
	Tile *t = getTile(IDtile);

	//cout << "x1:" << t->x1 << " || y1: " << t->y1 << " || x2: " << t->x2 << " || y2: " << t->y2 << " || idTile: " << IDtile << endl;
	glBegin(GL_QUADS);

	glTexCoord2f(t->x1,t->y2);
	glVertex2f(x1,y2);

	glTexCoord2f(t->x2,t->y2);
	glVertex2f(x2,y2);

	glTexCoord2f(t->x2,t->y1);
	glVertex2f(x2,y1);

	glTexCoord2f(t->x1,t->y1);
	glVertex2f(x1,y1);
	
	
	glEnd();
	}
TileSet::~TileSet(void)
	{
	//std::map<short,Tile*>::iterator it;
	//for (it = tiles.begin(); it != tiles.end(); ++it)
		//delete it->second;
	//tiles.clear();
	for (int i = 0;i < totalTiles; i++)
		delete tiles[i];
	delete []tiles;

	}
